package com.yrk.controller;

import com.github.pagehelper.Page;
import com.yrk.entity.Result;
import com.yrk.entity.User;
import com.yrk.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping("/toList")
    public ModelAndView toList(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/list");
        return mv;
    }

    @RequestMapping("/getPage")
    public Map getPage(int pageNum, int pageSize, User user){
        Map map = new HashMap();
        map.put("user", user);
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        Page page = userService.queryLikeUsers(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }
    @RequestMapping("/addUser")
    public Result addUser(User user){
        userService.addUser(user);
        return new Result();
    }
    //删除用户
    @RequestMapping("/deleteUser")
    public String deleteUser(int id){
       int num= userService.daleteUser(id);
       if(num>0){
           return "true";
       }else{
           return "false";
       }
    }
    @RequestMapping("/updatePage")
    public ModelAndView updatePage(int id){
        User user = userService.getUserById(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("data",user);
        mv.setViewName("/update");
        return mv;
    }
    @RequestMapping("/updateUser")
    public String updateUser(User user){
       int num= userService.updateUser(user);
       if(num>0){
           return "true";
       }else{
           return "false";
       }
    }
}
